package E2013;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class Exam5  {

    public void createAndShow(){

        JFrame frame = new JFrame("库存查询窗口");
        frame.setSize(400,300);
        frame.setLocation(300,200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel jPanel = new JPanel();
        JComboBox<String> jComboBox = new JComboBox<>();
        jComboBox.addItem("请选择商品");
        jComboBox.addItem("色拉油");
        jComboBox.addItem("齐心汽水");
        jComboBox.addItem("米酒");
        jComboBox.addItem("冰淇淋");
        jComboBox.addItem("蛋糕");

        JTextField jTextField1 = new JTextField(5);
        JLabel jLabel1 = new JLabel("单价");
        JTextField jTextField2 = new JTextField(5);
        JLabel jLabel2 = new JLabel("库存");

        jComboBox.addActionListener(e->{

                String goods = (String) jComboBox.getSelectedItem();
                if ("色拉油".equals(goods)){
                    jTextField1.setText("56");
                    jTextField2.setText("232");
                }else if ("齐心汽水".equals(goods)){
                    jTextField1.setText("8");
                    jTextField2.setText("50");
                }else if ("米酒".equals(goods)){
                    jTextField1.setText("10");
                    jTextField2.setText("109");
                }else if ("冰淇淋".equals(goods)){
                    jTextField1.setText("20");
                    jTextField2.setText("48");
                }else {
                    jTextField1.setText("90");
                    jTextField2.setText("30");
                }

        });

        jPanel.add(jComboBox);
        jPanel.add(jLabel1);
        jPanel.add(jTextField1);
        jPanel.add(jLabel2);
        jPanel.add(jTextField2);

        frame.add(jPanel, BorderLayout.PAGE_START);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Exam5 e = new Exam5();
                e.createAndShow();
            }
        });
    }

}
